import WeDisplayShaders from "./WeDisplayShaders"

export const fragmentMain = `
void fragmentMain(FragmentInput fsInput, inout czm_modelMaterial material)
        {
            vec3 str = fsInput.voxel.positionUvLocal;
            float minValue = Weget_minValue();
            float maxValue = Weget_maxValue();
            float filterMin = Weget_volumeFilterMin();
            float filterMax = Weget_volumeFilterMax();
            float clipMinX = Weget_clipMinMaxX().x;
            float clipMaxX = Weget_clipMinMaxX().y;
            float clipMinY = Weget_clipMinMaxY().x;
            float clipMaxY = Weget_clipMinMaxY().y;
            float clipMinZ = Weget_clipMinMaxZ().x;
            float clipMaxZ = Weget_clipMinMaxZ().y;
            bool shouldAnime = Weget_shouldAnime() > 0.0;
            float topAlpha = Weget_topAlpha();
            bool shouldFlipY = Weget_shouldFlipY() > 0.0;

            vec4 worldExtent = Weget_extent();
            vec2 lonLat = WeGetImageryLonLat(str.xy,u_localExtent);
            str.xy = WeGetImageryLocalUV(lonLat,worldExtent);
            if(shouldFlipY){
                str.y = 1.0 - str.y;
            }

            vec4 comp = WeDecodeVal(u_BaseTexture,str);
             if(shouldAnime){
                float animeStep = Weget_animeStep();
                vec4 lastComp = WeDecodeVal(u_LastBaseTexture,str);
                comp = mix(lastComp,comp,animeStep);
            }
            float value = comp.x;
            float i = (value - minValue) / (maxValue - minValue);
            vec4 color = texture(u_TFTexture,vec2(i,0.0));
            if(WeIsNodata(value,minValue,filterMin,filterMax)
            || str.x < clipMinX
            || str.y < clipMinY
            || str.z < clipMinZ
            || str.x > clipMaxX
            || str.y > clipMaxY
            || str.z > clipMaxZ){
              color.a = 0.0;
            }

            color.a *= i;

            if(topAlpha < str.z){
                color.a *= (1.0 - str.z);
            }

            float filterGap = Weget_filterGap();

            if(filterGap > 0.0)
            {
              float filterGapFactor = Weget_filterGapFactor();
              float rampV = filterGap;
              float clampV = mod(value,rampV) / rampV;
              float clampP = 1.0 - sin(clampV * czm_piOverTwo);
              color.rgb *=  pow(clampP,1.2);
              clampP = pow(clampP,filterGapFactor);
              color.a *= clampP;
            }

#ifdef ${WeDisplayShaders.WeDefineCalculateNormal3D}
            vec3 normalEC = normalize(comp.yzw);
            vec3 positionToEyeEC = fsInput.attributes.normalEC;
            czm_material material1;
            material1.diffuse = color.rgb;
            material1.specular = 0.0;
            material1.shininess = 0.0;
            material1.normal = -normalize(normalEC);
            material1.emission = vec3(0.1);
            material1.alpha = color.a;
            color = czm_phong(normalize(positionToEyeEC), material1, czm_lightDirectionEC);
#endif
            material.diffuse = color.rgb;
            material.alpha = color.a;

        }`

export default {}
